<template>
    <div class="timepicker">
        <div class="timepicker-header">
            <div class="timepicker-header-title">时</div>
            <div class="timepicker-header-title">分</div>
            <div class="timepicker-header-title">秒</div>
        </div>
        <div class="timepicker-content">
            <div class="hour timepicker-column" ref="hour" @scroll="scrollColumn($event,'hour')">
                <div class="item" v-for="(item, index) in hourList" :key="index" :class="{active: item.active}" @click="handleClick('hour', item)">{{item.text}}</div>
            </div>
            <div class="minute timepicker-column" ref="minute" @scroll="scrollColumn($event,'minute')">
                <div class="item" v-for="(item, index) in minuteList" :key="index" :class="{active: item.active}" @click="handleClick('minute', item)">{{item.text}}</div>
            </div>
            <div class="second timepicker-column" ref="second" @scroll="scrollColumn($event,'second')">
                <div class="item" v-for="(item, index) in secondList" :key="index" :class="{active: item.active}" @click="handleClick('second',item)">{{item.text}}</div>
            </div>
        </div>
    </div>
</template>


<script>

let itemHeight = 30;

export default{
    data(){
        let hourList = [];
        for(let i = 0; i < 24; i++){
            hourList.push({
                text: i<10 ? '0'+i : i,
                active: false
            })
        }

        let minuteList = [];
        for(let i = 0; i < 60; i++){
            minuteList.push({
                text: i < 10 ? '0'+i : i,
                active: false
            })
        }

        let secondList  = [];

        for(let i = 0; i < 60; i++){
            secondList.push({
                text: i < 10 ? '0'+i : i,
                active: false
            })
        }

        return {
            hourList,
            minuteList,
            secondList,
            timer:0
        }
    },

    props:{
        modelValue:{
            type:String,
            default:'00:00:00'
        },
        showHover:{
            type:Boolean,
            default:true
        },
        showMinute:{
            type:Boolean,
            default:true
        },
        showSecond:{
            type:Boolean,
            default:true
        },
    },
    emits:['change','update:modelValue'],
    computed:{
        hour:{
            get(){
                if (!this.modelValue){
                    return 0;
                }else{
                    return parseInt(this.modelValue.split(':')[0]??0)
                }
            },
            set(val){

                let hour = val<10?'0'+val:val;
                let minute = this.minute<10?'0'+this.minute:this.minute;
                let second = this.second<10?'0'+this.second:this.second;
                
                this.$emit('update:modelValue',`${hour}:${minute}:${second}`);
            }
        },
        minute:{
            get(){
                if (!this.modelValue){
                    return 0;
                }else{
                    return parseInt(this.modelValue.split(':')[1]??0)
                }
            },
            set(val){
                let hour = this.hour<10?'0'+this.hour:this.hour;
                let minute = val<10?'0'+val:val;
                let second = this.second<10?'0'+this.second:this.second;

                this.$emit('update:modelValue',`${hour}:${minute}:${second}`);
            }
        },
        second:{
            get(){
                if (!this.modelValue){
                    return 0;
                }else{
                    return parseInt(this.modelValue.split(':')[2]??0)
                }
            },
            set(val){
                let hour = this.hour<10?'0'+this.hour:this.hour;
                let minute = this.minute<10?'0'+this.minute:this.minute;
                let second = val<10?'0'+val:val;

                this.$emit('update:modelValue',`${hour}:${minute}:${second}`);
            }
        }
        
    },
    methods:{
        handleClick(type,item){
            let time  = parseInt(item.text);
            this.setValue(type,time);
            this.moveTime(type,time);
        },
        scrollColumn(event,type){
            let time  = this.getTime(event.target.scrollTop);
           
            clearTimeout(this.timer);

            this.timer = setTimeout(()=>{
                event.target.scrollTop = time * itemHeight;
            },300);
            
            this.setValue(type,time);
            this.updateList(type,time);
        },
        setValue(type,time){
            switch(type){
                case 'hour':
                    this.hour = time;
                break;
                case 'minute':
                    this.minute = time;
                break;
                case 'second':
                    this.second = time;
                break;
            }
        },
        moveTime(type,time){
            this.$refs[type].scrollTop = time*itemHeight;
            this.updateList(type,time);
        },
        getTime(top){
            return  Math.round((top)/itemHeight);
        },
        updateList(type,time){
            switch(type){
                case 'hour':
                if (time>24) time = 23;
                break;
                case 'minute':
                if (time>60) time = 59;
                break;
                case 'second':
                if (time>60) time = 59;
                break;
            }
            
            this.$data[type+'List'].map((item,index)=>{
                if (index == time) {
                    item.active = true;
                }else{
                    item.active = false;
                }
                return item;
            });
        }
    },
    mounted(){
        setTimeout(()=>{
            this.moveTime('hour',this.hour??0);
            this.moveTime('minute',this.minute??0);
            this.moveTime('second',this.second??0);
        },100);
        
    }
}
</script>


<style>
.timepicker{
    user-select: none;
}
.timepicker .timepicker-header{
    display: flex;
    background-color: #FFFFFF;
    position: relative;
    gap:10px;
    margin-bottom: 5px;
}
.timepicker .timepicker-header .timepicker-header-title{
    text-align: center;
    font-size: 12px;
    color:var( --bs-gray-600 );
    flex: 1;
}

.timepicker .timepicker-content{
    display: flex;
    background-color: #FFFFFF;
    justify-content: space-between;
    position: relative;
}

.timepicker .timepicker-content::before{
    content: '';
    position: absolute;
    width:100%;
    height: 1px;
    background-color: #E5E5E5;
    top: 90px;
    left: 0;
}

.timepicker .timepicker-content::after{
    content: '';
    position: absolute;
    width:100%;
    height: 1px;
    background-color: #E5E5E5;
    top: 120px;
    left: 0;
}

.timepicker .timepicker-content .timepicker-column{
    display: flex;
    flex: 1;
    flex-direction: column;
    height: 210px;
    overflow-y: scroll;
    padding-top: 90px;
    padding-bottom: 90px;
}

.timepicker .timepicker-content .timepicker-column .item{
    color:var( --bs-gray-600 );
    height: 30px;
    line-height: 30px;
    text-align: center;
    user-select: none;
    font-family: Arial, sans-serif;
    cursor: pointer;
    font-size: 12px;
}

.timepicker .timepicker-content .timepicker-column .item:hover{
    background-color: rgba(var(--bs-secondary-color-rgb), 0.04);

}
.timepicker .timepicker-content .timepicker-column .item.active{
    color:var( --bs-primary );
    font-weight: bold;
    background-color: transparent;
    font-size: 14px;
}
</style>